#include <cstdlib>
#include <iostream>
#include "Node.h"

template <typename T>
class LinkedList
{
private:  
    Node<T>* head;
public:
    LinkedList();
    ~LinkedList();
    Node<T>* get_head() const;
    void append(const Node<T>& _node);
    void append(int _end, T _weight);
    void list() const;
};

template <typename T>
Node<T>* LinkedList<T>::get_head() const
{
  return head;
}

template <typename T>
LinkedList<T>::LinkedList()
{
    head = NULL;
};

template <typename T>
LinkedList<T>::~LinkedList()
{
    if (head != NULL)
    {
	Node<T>* p = head;
	while (p->get_next() != NULL)
	{
	    Node<T>* t = p;
	    p = p->get_next();
	    delete t;
	}
	delete p;
    }
};

template <typename T>
void LinkedList<T>::append(const Node<T>& _node)
{
    Node<T>* tail = head;
    Node<T>* p = new Node<T>;
    p->set_end(_node.get_end());
    p->set_weight(_node.get_weight());
    p->set_next(NULL);
    if (tail != NULL)
    {
    while (tail->get_next() != NULL)
	tail = tail->get_next();
    tail->set_next(p);
    }
    else
	head = p;
};

template <typename T>
void LinkedList<T>::append(int _end, T _weight)
{
    Node<T>* tail = head;
    Node<T>* p = new Node<T>;
    p->set_end(_end);
    p->set_weight(_weight);
    p->set_next(NULL);
    if (tail != NULL)
    {
    while (tail->get_next() != NULL)
	tail = tail->get_next();
    tail->set_next(p);
    }
    else
	head = p;
};

template <typename T>
void LinkedList<T>::list() const
{
    Node<T>* tail = head;
    if (tail != NULL)
    {
	while (tail->get_next() != NULL)
	{
	  std::cout << "to " << tail->get_end() << ",w=" << tail->get_weight()<< "; ";
	    tail = tail->get_next();
	}
	std::cout << "to " << tail->get_end() << ",w=" << tail->get_weight()<< ";" << std::endl;
    }
    else
      std::cout << "No Side!" << std::endl;
};
